
package taller2.controller.modes;

import java.awt.event.MouseEvent;

import taller2.interfaces.Graph;
import taller2.interfaces.Node;
import taller2.utils.Point;
import taller2.view.draws.DrawingArea;
import taller2.view.draws.GraphDrawer;

public class SelectConnectionsMode implements Mode {
	private Graph graph;
	private Node selected;
	
	// constructor
	public SelectConnectionsMode(Graph graph){
		this.graph = graph;
		this.selected = null;
	}
	
	
	public void whenPressed(MouseEvent arg0, DrawingArea area) {
		GraphDrawer drawer = area.getDrawer();
		Point pos = new Point(arg0.getX(),arg0.getY());
		
		if(selected == null){
			selected = drawer.getCloseByNode(pos);			
		}else{
			Node start = selected;
			Node end = drawer.getCloseByNode(pos);
			if(end != null){
				if(graph.isDirected()){
					start.selectConnectionTo(end);
				}else{
					start.selectConnectionsWith(end);					
				}
				
				start.select();
				end.select();
			}	
			selected = null;
		}	
		
		area.repaint();
	}
		
	public void whenReleased(MouseEvent arg0, DrawingArea area) {}

	public void whenDragged(MouseEvent arg0, DrawingArea area) {}

}
